package com.wb3.organizer.action;

import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.apache.struts2.interceptor.SessionAware;

import com.opensymphony.xwork2.ActionSupport;
import com.wb3.organizer.model.Account;
import com.wb3.organizer.model.Contact;
import com.wb3.organizer.service.ContactService;

public class ContactAction extends ActionSupport implements SessionAware {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private Logger log = Logger.getLogger(ContactAction.class);

	private Map<String, Object> session;

	private ContactService service;

	private Contact contact;

	private List<Contact> contacts;

	private String operate;

	private String msg;

	public void setSession(Map<String, Object> inSession) {
		this.session = inSession;
	}

	public ContactService getService() {
		return service;
	}

	public void setService(ContactService service) {
		this.service = service;
	}

	public Contact getContact() {
		return contact;
	}

	public void setContact(Contact contact) {
		this.contact = contact;
	}

	public List<Contact> getContacts() {
		return contacts;
	}

	public void setContacts(List<Contact> contacts) {
		this.contacts = contacts;
	}

	public String getOperate() {
		return operate;
	}

	public void setOperate(String operate) {
		this.operate = operate;
	}

	public String getMsg() {
		return msg;
	}

	public void setMsg(String msg) {
		this.msg = msg;
	}
	
	public String create(){
		log.info("\n\n----------------------------------------------------------\n");
		log.debug("ContactAction.create()...\n");
		
		Account account = (Account) session.get("account");
		contact.setAccount(account);
		contact.setCreatedDT(new Date().getTime());
		service.saveContact(contact);
		
		this.setOperate("contactCreateOK");
		this.setMsg("联系人创建成功!!!");
		log.debug("ContactAction.create() Done\n");
		return SUCCESS;
	}
	
	public String update(){
		log.info("\n\n----------------------------------------------------------\n");
		log.debug("ContactAction.update()...\n");
	
		Account account = (Account) session.get("account");
		contact.setAccount(account);
		contact.setCreatedDT(new Date().getTime());
		service.updateContact(contact);
		
		this.setOperate("contactUpdateOK");
		this.setMsg("联系人修改成功!!!");
		log.debug("ContactAction.update() Done\n");
		return SUCCESS;
	}
	
	public String delete(){
		log.info("\n\n----------------------------------------------------------\n");
		log.debug("ContactAction.delete()...\n");
		
		service.deleteContact(contact);
		
		this.setOperate("contactDeleteOK");
		this.setMsg("联系人删除成功!!!");
		log.debug("ContactAction.delete() Done\n");
		return SUCCESS;
	}

	public String list() {
		log.info("\n\n----------------------------------------------------------\n");
		log.debug("ContactAction.list()...\n");

		Account account = (Account) session.get("account");
		contacts = service.listContact(account);

		log.debug("ContactAction.list() Done\n");
		return SUCCESS;
	}
	
	public String forward() {
		if (operate == null) {
			return SUCCESS;
		} else {
			if (operate.equals("create")) {
				return "create";
			} else if (operate.equals("update")) {
				contact = service.getContact(contact.getCon_id());
				return "update";
			} else if (operate.equals("delete")) {
				return "delete";
			} else {
				return ERROR;
			}
		}
	}
}
